package com.atyoung.youngmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atyoung.youngmall.product.entity.BrandEntity;
import com.atyoung.youngmall.product.service.BrandService;
import com.atyoung.youngmall.product.service.CategoryService;
import com.atyoung.youngmall.product.service.impl.BrandServiceImpl;
import com.atyoung.youngmall.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atyoung.youngmall.product.entity.CategoryBrandRelationEntity;
import com.atyoung.youngmall.product.service.CategoryBrandRelationService;
import com.atyoung.common.utils.PageUtils;
import com.atyoung.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author wen_chuang
 * @email wen_chuang@gmail.com
 * @date 2020-05-06 22:13:48
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @GetMapping("/catelog/list")
    public R brandList(@RequestParam Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> qw =  new QueryWrapper<CategoryBrandRelationEntity>();
        qw.eq("brand_id",brandId);
        List<CategoryBrandRelationEntity> list =  categoryBrandRelationService.list(qw);
        return R.ok().put("data",list);
    }

    @PostMapping("/save")
    public R saveDetail(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
       categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    //获取分类关联的品牌
    @GetMapping("/brands/list")
    public R brandsList(@RequestParam(value = "catId", required = true) Long catId){
        List<BrandEntity> list = categoryBrandRelationService.getBrandsList(catId);

        List<BrandVo> collect = list.stream().map(entity -> {
            BrandVo brandVo = new BrandVo();
            Long brandId = entity.getBrandId();
            String brandName = entity.getName();
            brandVo.setBrandName(brandName);
            brandVo.setBrandId(brandId);
            return brandVo;
        }).collect(Collectors.toList());
            return R.ok().put("data",collect);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

   /* *//**
     * 保存
     *//*
    @RequestMapping("/save")
   // @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }*/

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
